﻿using HookupAppForm.Draw.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HookupAppForm.Draw
{
    /// <summary>
    /// UC_CustomDrawList.xaml 的交互逻辑
    /// </summary>
    public partial class UC_CustomDrawList : UserControl
    {
        FormMain mainForm;
        public UC_CustomDrawList(FormMain _main)
        {
            InitializeComponent();
            Loaded += UC_CustomDrawList_Loaded;
            mainForm = _main;
        }

        private void UC_CustomDrawList_Loaded(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p== "DESIGNER");
            if (a <= 0)
            {
                return;
            }
            LoadData();
        }

        private void btn_search_click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            LoadData();
        }

        private void btn_delshape_click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            var item = (sender as Button).DataContext as CustomDrawInfo;
            DelData(new List<CustomDrawInfo>() { item });
        }

        private void btn_editshape_click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            var item = (sender as Button).DataContext as CustomDrawInfo;
            FrmWpfContainer.ShowDialog(new UC_DrawEdit(item), "", System.Windows.Forms.FormWindowState.Normal, 1512, 930);
            LoadData();
        }

        private void btn_addnew_click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            FrmWpfContainer.ShowDialog(new UC_DrawEdit(new CustomDrawInfo()), "", System.Windows.Forms.FormWindowState.Normal, 1512, 930);
            LoadData();
        }

        private void DelData(List<CustomDrawInfo> list)
        {
            if (MessageBox.Show("数据删除后将不可恢复，是否继续？", "提示", MessageBoxButton.YesNo) != MessageBoxResult.Yes) return;

            //接口执行删除  
            var ok = LocalDataHelper.DeleteShape(list.First().uid);
            if (!ok)
            {
                MessageBox.Show("数据删除失败！请稍后重试！");
                return;
            }

            //刷新数据
            LoadData();
        }

        private void LoadData()
        {
            Task.Run(() =>
            {
                Thread.Sleep(50);

                try
                {
                    var str_search_shapename = "";
                    tb_search_shapname.Dispatcher.Invoke(delegate
                    {
                        str_search_shapename = tb_search_shapname.Text;
                    });
                    //从接口获取数据   
                    var lst = LocalDataHelper.GetShapeList(this, str_search_shapename); 
                    lst = lst.Where(a => a.ShapeName.Contains(str_search_shapename)).ToList();

                    //填充数据 
                    dg1.Dispatcher.Invoke(delegate
                    {
                        dg1.ItemsSource = lst;
                    });

                    lv1.Dispatcher.Invoke(delegate
                    {
                        lv1.ItemsSource = lst;
                    });
                }
                catch (Exception ex)
                {

                }
            });
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var item = (sender as Grid).DataContext as CustomDrawInfo;
            FrmWpfContainer.ShowDialog(new UC_DrawEdit(item), "", System.Windows.Forms.FormWindowState.Normal, 1512, 930);
            LoadData();
        }

        private void rbSmall_Click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            dg1.Visibility = Visibility.Visible;
            lv1.Visibility = Visibility.Collapsed;
        }

        private void rbLarge_Click(object sender, RoutedEventArgs e)
        {
            string[] str = mainForm.user[0].RoleNames;
            int a = str.Count(p => p == "ADMIN" || p == "DESIGNER");
            if (a <= 0)
            {
                Utils.SysMsg("对不起！权限不足，请联系管理员！");
                return;
            }
            dg1.Visibility = Visibility.Collapsed;
            lv1.Visibility = Visibility.Visible;
        }
    }
}
